<?php

namespace app\api\controller;

use app\api\model\Banner as BannerModel;

/**
 * 团队控制器
 * Class Leader
 * @package app\api\controller
 */
class Banner extends Controller
{
    private $user;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->user = $this->getUser();
    }

    /**
     *
     * @return array
     */
//    public function index()
//    {
//        // 领导团队列表
//        $categoryList = CategoryModel::where("shanghui_id", $this->user['shanghui_id'])->order("sort asc")->select();
//        return $this->renderSuccess(compact('categoryList'));
//    }

    /**
     * Banner列表
     * @return array
     * @throws \think\exception\DbException
     */
    public function lists()
    {
        $model = new BannerModel;
        $list = $model->getList(1000, $this->user['shanghui_id']);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 团队详情
     * @param $Banner_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function detail($banner_id)
    {
        $detail = BannerModel::detail($banner_id);
        return $this->renderSuccess(compact('detail'));
    }


    /****************************************************************************
     * 添加banner
     * @param $banner_id
     */
    public function add()
    {
        $param = input('param.');
        if($param['banner_id']){
            $data =[
                'image_id' => $param['image_id'],
                'sort' => $param['sort'],
                'update_time' => time(),
                'user_id' => $this->user['user_id'],
            ];
            BannerModel::where('id',$param['banner_id'])->update($data);
            return $this->renderSuccess([], 'banner修改成功');
        }
        $data = [
            'image_id' => $param['image_id'],
            'shanghui_id' => $this->user['shanghui_id'],
            'wxapp_id' => $this->user['wxapp_id'],
            'sort' => $param['sort'],
            'user_id' => $this->user['user_id'],
            'update_time' => time(),
            'create_time' => time(),
        ];
        BannerModel::insert($data);
        return $this->renderSuccess([], 'banner添加成功');
    }

    /****************************************************************************
     * 删除banner，banner不需要编辑，不爽就删
     * @param $banner_id
     */
    public function del()
    {
        $param = input('param.');
        BannerModel::where('shanghui_id', $this->user['shanghui_id'])->where('id', $param['id'])->delete();
        return $this->renderSuccess([], 'banner删除成功');
    }

}
